<template>
	<div ref="mgrDialog" class="dialog-wrap">
		  
		<el-dialog 
			:title="headerTitle" 
			width="40%"
			@close="resetDialog" 
			:visible.sync="DialogVisible"
		>
			
			<el-row :gutter="20">

				<el-form 
					v-if = "editForm != null"
					ref="dialogForm"
					:rules="rules"
					size="mini"
					:label-width="formLabelWidth"
					:model.sync="editForm">
					
					<el-col :span="22">
						<el-form-item label="设备类型" prop="type">
			    			<el-select v-model="editForm.type" placeholder="请选择设备类型">
							    <el-option
							      v-for="item in types"
							      :key="item.value"
							      :label="item.label"
							      :value="item.value">
							    </el-option>
							</el-select>
						</el-form-item>
		    		</el-col>

		    		<el-col :span="22">	
		    			<el-form-item label="设备编号" prop="number">
		      				<el-input v-model="editForm.number" autocomplete="off" placeholder="12位设备编号"></el-input>
		    			</el-form-item>
		    		</el-col>

		    		<el-col :span="22">	
		    			<el-form-item label="设备密码" prop="password">
		      				<el-input v-model="editForm.password" autocomplete="off" placeholder="任意字符没有可不填"></el-input>
		    			</el-form-item>
		    		</el-col>

		    		<el-col :span="22">	
		    			<el-form-item label="下发指令" prop="pid">

		    				<el-select v-model="editForm.type" placeholder="请选择下发指令">
							    <el-option
							      v-for="item in driectives"
							      :key="item.value"
							      :label="item.label"
							      :value="item.value">
							    </el-option>
							</el-select>

		    			</el-form-item>
		    		</el-col>

		    		<el-col :span="22">	
		    			<el-form-item label="数值" prop="value">
		      				<el-input v-model="editForm.value" type="number" autocomplete="off" placeholder="请输入数值"></el-input>
		    			</el-form-item>
		    		</el-col>
		    			
				</el-form>
			</el-row>
			
			<div slot="footer" class="dialog-footer">
				<el-button size="mini" type="primary" @click="confirm">确定</el-button>
			    <el-button size="mini" type="danger" @click="cancel">取消</el-button>
			</div>	
		</el-dialog>

	</div>
</template>


<script>
import { getRolesTree,getRoleTreeList } from "@/api/admin/roles"
import { getUserInfo } from "@/api/user"
import { convertDataToTree } from "@/utils/util"
import InputTree from "@/components/InputTree"
export default{
	props:{
		//新增还是编辑
	    type:{
	      type:[Number,String],
	      defalut:1
	    },
		visible:{
			type:Boolean,
			default:false
		},
		form:{
			type:Object,
			default:null
		}
	},
	components:{
    	InputTree
  	},
	watch:{
		form(newVal,oldVal){
			if(newVal != null){
				this.editForm = newVal;
			}else{
				this.editForm = this.defaultForm;
			}	
		},
		type(newVal,oldVal){
	      	if(newVal == 1){
	        	this.headerTitle = "下发指令"
	      	}else if(newVal == 2){
	        	this.headerTitle = "下发指令"
	      	}
	    },
	    visible(val){
	    	this.DialogVisible = val;
	    	if(this.form === null){
	    		this.editForm = this.defaultForm;
	    	}
	    }
	},
	data(){
		return{
			headerTitle:"下发指令",
			DialogVisible:false,
			defaultForm:{
				type:'',
				number:'',
				password:'',
				driective:'',
				value:''
			},
			roleTree:[],
			types:[{
				label:'NB设备',
				value:1
			},{
				label:'网络继电器',
				value:2
			},{
				label:'噪声扬尘',
				value:3
			},{
				label:'智慧井盖',
				value:4
			},{
				label:'一键报警器',
				value:5
			}],
			driectives:[{
				label:'采集间隔（单位：10s）',
				value:1
			},{
				label:'上传间隔（单位：10s）',
				value:2
			},{
				label:'command12（单位：0.1）',
				value:3
			},{
				label:'command13（单位：0.1）',
				value:4
			},{
				label:'command14（单位：0.1）',
				value:5
			}],
			testEquipmentGroup:[
				{
					checked: true, 
					iconSkin: null, 
					id: 0, 
					name: "根目录", 
					open: true, 
					pId: 0
				},
				{
					checked: null, 
				 	iconSkin: null, 
				 	id: 1, 
				 	name: "继电器设备", 
				 	open: true, 
				 	pId: 0
				},
				{
					checked: null, 
				 	iconSkin: null, 
				 	id: 2, 
				 	name: "采集设备", 
				 	open: true, 
				 	pId: 0
				}
			],
			roleTreeProps:{
        		children: 'childrenNodes',
        		label: 'name',
        		value: 'id'
      		},
			editForm:null,
			formLabelWidth:'100px',
			//验证规则
		    rules:{
		        type:[{required:true,message:"请选择设备类型",trigger:"blur"}],
		        number:[{required:true,message:"请输入设备编号",trigger:"blur"}],
		        password:[{required:true,message:"请输入设备密码",trigger:"blur"}],
		        driective:[{required:true,message:"请选择下发指令",trigger:"blur"}],
		        value:[{required:true,message:"请输入数值",trigger:"blur"}],
		    },
		}
	},
	mounted(){
		this.getRoleTree();
	},
	methods:{
		//获取树形菜单
		getRoleTree(){

			getRoleTreeList().then((res)=>{
				console.log("-----------获取树形菜单-----------")
				console.log(res);
				//this.roleTree = convertDataToTree(res.data);
				this.roleTree = convertDataToTree(this.testEquipmentGroup)
			})

		},
		resetDialog(){
			this.DialogVisible = false;
			this.editForm = null;
			this.$emit('cancel')
		},
		//上级角色选择
		roleSelect(data){
			this.editForm.pid = data.id;
		},
		//取消按钮
		cancel(){
			this.DialogVisible = false;
			this.editForm = null;
			this.$emit('cancel')
		},
		confirm(){
			this.$refs.dialogForm.validate((valid)=>{
		        if (valid) {
		          this.DialogVisible = false;

		          if(this.editForm.pid === 0){
		          	this.editForm.pid = '0';
		          }

		          this.$emit('confirm',{type:this.type,data:this.editForm});

		        } else {
		           
		            return false;
		        }

      		})
		}

	}
}


</script>

<style scoped>
	.with-full{
	    width:100%!important;
	}
</style>